// pages/agentOrders/list/list.js
const common = require('../../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        orderType: 0,
        orderList: [],
        nowPage: 0,
        isLoading: false,
        isAll: false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getOrderList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.setData({
            orderList: [],
            nowPage: 0,
            isLoading: false,
            isAll: false
        })
        common.refresh()
        setTimeout(function () {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.getOrderList()
    },

    getOrderList: function () {
        if (this.data.isLoading) {
            return
        }
        if (this.data.isAll) {
            return
        }
        const that = this
        let data
        let orderList = this.data.orderList
        if (this.data.orderType == 0) {
            data = {
                get_user: 1,
                paramter: 1,
                goods_client: 2,
                goods_store_id: 2,
                limit: 10,
                page: this.data.nowPage
            }
        } else if (this.data.orderType == 1) {
            data = {
                get_user: 1,
                paramter: 1,
                goods_client: 2,
                goods_store_id: 2,
                order_status: 0,
                shipping_status: 0,
                pay_status: 0,
                limit: 10,
                page: this.data.nowPage
            }
        } else if (this.data.orderType == 2) {
            data = {
                get_user: 1,
                paramter: 1,
                goods_client: 2,
                goods_store_id: 2,
                order_status: 0,
                shipping_status: 0,
                pay_status: 2,
                limit: 10,
                page: this.data.nowPage
            }
        } else if (this.data.orderType == 3) {
            data = {
                get_user: 1,
                paramter: 1,
                goods_client: 2,
                goods_store_id: 2,
                order_status: 0,
                shipping_status: 1,
                pay_status: 2,
                limit: 10,
                page: this.data.nowPage
            }
        } else if (this.data.orderType == 4) {
            data = {
                get_user: 1,
                paramter: 1,
                goods_client: 2,
                goods_store_id: 2,
                order_status: 1,
                shipping_status: 2,
                pay_status: 2,
                limit: 10,
                page: this.data.nowPage
            }
        } else if (this.data.orderType == 5) {
            data = {
                get_user: 1,
                paramter: 1,
                goods_client: 2,
                goods_store_id: 2,
                order_status: 2,
                limit: 10,
                page: this.data.nowPage
            }
        }
        this.setData({
            isLoading: true
        })
        wx.showLoading({
            title: '加载中',
        })
        common.post('Orderlist/user_order', data).then(function (res) {
            that.setData({
                isLoading: false
            })
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    orderList: orderList,
                    isAll: true
                })
            } else {
                orderList = orderList.concat(res)
                that.setData({
                    orderList: orderList,
                    nowPage: that.data.nowPage + 1
                })
            }
        }).catch(function (e) { })
    },

    switchOrderType: function (e) {
        if (this.data.orderType == e.currentTarget.dataset.type) {
            return
        }
        this.setData({
            orderType: e.currentTarget.dataset.type,
            nowPage: 0,
            isAll: false,
            isLoading: false,
            orderList: []
        })
        this.getOrderList()
    },

    goOrderDetail: function (e) {
        wx.navigateTo({
            url: '/pages/agentOrders/detail/detail?orderId=' + e.currentTarget.dataset.id
        })
    },

    goLogistics: function (e) {
        wx.navigateTo({
            url: '/pages/order/orderLogistics/orderLogistics?expressId=' + e.currentTarget.dataset.id
        })
    },

    goGoodsDetail: function (e) {
        wx.navigateTo({
            url: '/pages/goods/goodsDetail/goodsDetail?goodsId=' + e.currentTarget.dataset.id
        })
    },

    updateData: function () {
        const that = this
        let data
        if (this.data.orderType == 0) {
            data = {
                get_user: 1,
                paramter: 1,
                goods_client: 2,
                goods_store_id: 2,
                limit: this.data.nowPage * 10,
                page: 0
            }
        } else if (this.data.orderType == 1) {
            data = {
                get_user: 1,
                paramter: 1,
                goods_client: 2,
                goods_store_id: 2,
                order_status: 0,
                shipping_status: 0,
                pay_status: 0,
                limit: this.data.nowPage * 10,
                page: 0
            }
        } else if (this.data.orderType == 2) {
            data = {
                get_user: 1,
                paramter: 1,
                goods_client: 2,
                goods_store_id: 2,
                order_status: 0,
                shipping_status: 0,
                pay_status: 2,
                limit: this.data.nowPage * 10,
                page: 0
            }
        } else if (this.data.orderType == 3) {
            data = {
                get_user: 1,
                paramter: 1,
                goods_client: 2,
                goods_store_id: 2,
                order_status: 0,
                shipping_status: 1,
                pay_status: 2,
                limit: this.data.nowPage * 10,
                page: 0
            }
        } else if (this.data.orderType == 4) {
            data = {
                get_user: 1,
                paramter: 1,
                goods_client: 2,
                goods_store_id: 2,
                order_status: 1,
                shipping_status: 2,
                pay_status: 2,
                limit: this.data.nowPage * 10,
                page: 0
            }
        } else if (this.data.orderType == 5) {
            data = {
                get_user: 1,
                paramter: 1,
                goods_client: 2,
                goods_store_id: 2,
                order_status: 2,
                limit: this.data.nowPage * 10,
                page: 0
            }
        }
        wx.showLoading({
            title: '加载中',
        })
        common.post('Orderlist/user_order', data).then(function (res) {
            wx.hideLoading()
            if (res.code == 204) {
                that.setData({
                    orderList: []
                })
            } else {
                that.setData({
                    orderList: res
                })
            }
        }).catch(function (e) { })
    }
})